import Util from "./../util/Util";

/**
 * 按钮
 */
export default class Btn extends Laya.Script {
    constructor() {
        super();
    }
    onEnable() {
        // this.owner
        //点击放大
        var scaleTime = 100;
        var istag = false;
        var btn = this.owner;
        btn.tapcall = undefined;
        var noscale = false;
        var maxscale = 1;

        this.scaleBig = function () {
            if (istag) {
                istag = false;
                if (!noscale) {
                    //变大还原的缓动效果
                    Laya.Tween.to(btn, {
                        scaleX: maxscale,
                        scaleY: maxscale
                    }, scaleTime, Laya.Ease.linearNone, new Laya.Handler(this, this.tapfun));
                } else {
                    this.tapfun();
                }
            }
        }
        this.scaleBig2 = function () {
            if (istag) {
                istag = false;
                if (!noscale) {
                    //变大还原的缓动效果
                    Laya.Tween.to(btn, {
                        scaleX: maxscale,
                        scaleY: maxscale
                    }, scaleTime, Laya.Ease.linearNone);
                }
            }
        }
        this.scaleSmal = function () {
            if (!istag) {
                istag = true;
                if (!noscale) {
                    //缩小至0.8的缓动效果
                    Laya.Tween.to(btn, {
                        scaleX: maxscale * 1.2,
                        scaleY: maxscale * 1.2
                    }, scaleTime);
                    // Util.playSound("tap");
                }
            }
        }
        //点击回调
        this.tapfun = function () {
            // console.log("点击回调");
            if (btn.tapcall) {
                btn.tapcall(btn.tapcallprop);
            }
        }
        btn.setNoScale = function () {
            noscale = true;
            return btn;
        }
        btn.setTapCall = function (call, prop) {
            btn.tapcall = call;
            btn.tapcallprop = prop;
        }
        btn.setMaxScale = function (maxscale_) {
            maxscale = maxscale_;
        }



        if (this.owner.on) {
            //添加鼠标按下事件侦听。按时时缩小按钮。
            this.owner.on(Laya.Event.MOUSE_DOWN, this, this.scaleSmal);
            //添加鼠标抬起事件侦听。抬起时还原按钮。
            this.owner.on(Laya.Event.MOUSE_UP, this, this.scaleBig);
            //添加鼠标离开事件侦听。离开时还原按钮。
            this.owner.on(Laya.Event.MOUSE_OUT, this, this.scaleBig2);
        }
    }

    onUpdate() {

    }


    onDisable() {
        if (this.owner.off) {
            //添加鼠标按下事件侦听。按时时缩小按钮。
            this.owner.off(Laya.Event.MOUSE_DOWN, this, this.scaleSmal);
            //添加鼠标抬起事件侦听。抬起时还原按钮。
            this.owner.off(Laya.Event.MOUSE_UP, this, this.scaleBig);
            //添加鼠标离开事件侦听。离开时还原按钮。
            this.owner.off(Laya.Event.MOUSE_OUT, this, this.scaleBig2);
        }
    }
}